/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: GameCreationScreen.java 645 2010-07-30 05:58:23Z Harry $
 *   $LastChangedDate: 2010-07-30 05:58:23 +0000 (Fri, 30 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.screen;

import battlefield.resource.Graphics;
import battlefield.entity.Player;
import battlefield.ui.component.NormalWhiteButton;
import battlefield.util.ComponentDecorator;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Yong Peng
 */
public class GameCreationScreen extends Screen implements ActionListener {

    JLabel createGameLab;
    JLabel ipAddressLab;
    JLabel goldReloadDurationLab;
    JLabel goldReloadAmtLab;
    JLabel startGoldLab;
    JTextField ipAddressTxt;
    JTextField goldReloadDurationTxt;
    JTextField goldReloadAmtTxt;
    JTextField startGoldTxt;
    JButton nextBut;
    JButton backBut;
    private String defaultGoldDuration="5", defaultGoldReload="2000",defaultStartGold="10000";

    public GameCreationScreen() {
        initRMIServerClient();
        initComponents();
    }

    private void initComponents() {
        createGameLab = new JLabel(new ImageIcon(Graphics.SCREEN_TITLE_CREATE_GAME.getImage()));
        ipAddressLab = ComponentDecorator.decorateLabel(ipAddressLab, "Your IP Address");
        goldReloadDurationLab = ComponentDecorator.decorateLabel(goldReloadDurationLab, "Gold Reload Duration");
        goldReloadAmtLab = ComponentDecorator.decorateLabel(goldReloadAmtLab, "Gold per Reload");
        startGoldLab = ComponentDecorator.decorateLabel(startGoldLab, "Start Gold");
        ipAddressTxt = ComponentDecorator.decorateTextfield(ipAddressTxt, 15, getSession().getIpAddress());
        goldReloadDurationTxt = ComponentDecorator.decorateTextfield(goldReloadDurationTxt, 2, defaultGoldDuration);
        goldReloadAmtTxt = ComponentDecorator.decorateTextfield(goldReloadAmtTxt, 6, defaultGoldReload);
        startGoldTxt = ComponentDecorator.decorateTextfield(goldReloadAmtTxt, 6, defaultStartGold);
        backBut = new NormalWhiteButton("Back", this);
        nextBut = new NormalWhiteButton("Next", this);
        this.setBackgroundImage(Graphics.BACKGROUND_MISC.getImage());
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(
                layout.createSequentialGroup().addGap(50, 50, 50).addGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(createGameLab).addGroup(layout.createSequentialGroup().addComponent(ipAddressLab).addComponent(ipAddressTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addComponent(goldReloadDurationLab).addComponent(goldReloadDurationTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addComponent(goldReloadAmtLab).addComponent(goldReloadAmtTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addComponent(startGoldLab).addComponent(startGoldTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addComponent(backBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(nextBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))));
        layout.linkSize(ipAddressLab, goldReloadDurationLab, goldReloadAmtLab, startGoldLab);
        layout.setVerticalGroup(
                layout.createSequentialGroup().addGap(50, 50, 50).addComponent(createGameLab).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(ipAddressLab).addComponent(ipAddressTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(goldReloadDurationLab).addComponent(goldReloadDurationTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(goldReloadAmtLab).addComponent(goldReloadAmtTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(startGoldLab).addComponent(startGoldTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(backBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(nextBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)));
    }

    private void initRMIServerClient() throws HeadlessException {
        try {
            getNetwork().startServer();
            InetAddress addr = InetAddress.getLocalHost();
            getSession().setIpAddress(addr.getHostAddress());
            getNetwork().startClient(addr.getHostAddress());
            Player player = getLogic().registerPlayer();
            player.setName(getSession().getPlayerName());
            getSession().setPlayer(player);
            getLogic().changePlayerName(getPlayer().getId(), player.getName());
        } catch (NamingException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        } catch (java.net.UnknownHostException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /* Begin Region: State Pattern */
    @Override
    public Screen chooseMap() {
        try {
            getLogic().createGame(getPlayer().getId());
            getSession().setGoldReloadDuration(Integer.parseInt(goldReloadDurationTxt.getText()));
            getSession().setStartGold(Integer.parseInt(startGoldTxt.getText()));
            getSession().setGoldReloadAmount(Integer.parseInt(goldReloadAmtTxt.getText()));
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
        return new MapChooserScreen();
    }

    @Override
    public Screen back() {
        getNetwork().stop();
        return new GameMenuScreen();
    }
    /* End Region: State Pattern */

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(nextBut)) {
            getGUI().chooseMap();
        } else if (e.getSource().equals(backBut)) {
            getGUI().back();
        }
    }
}
